<?php
require_once(dirname(__FILE__)."/utils.php");

class JEngBannerProcessor extends JEngUtilHelper
{
  private $site=null;
  private $logger=null;

  function __construct($utils)
  {
    parent::__construct($utils);
	$this->site=$utils->site();
	$this->logger=$utils->logger();
  }
  
  function replaceBanner($xml,$place_name,$obj)
  {
	if ($obj==null)
	{
	  $obj=$this->site->emptyXML("banner");
	}
	JEngXCombiner::s_render($xml,$obj,$place_name,"banner",
		JEngXCombiner::HOW_ONCE | JEngXCombiner::HOW_REPLACE);
	return 0;
  }
  
  function queryBannerFields($id)
  {
    $res=$this->site->common_db()->queryf("SELECT link,image,flv,adata FROM banners WHERE id=%d;",$id);
	if (!$res) return null;
	return $res->next();
  }

  function queryPlaceFields($id)
  {
    $res=$this->site->common_db()->queryf("SELECT class,width,height FROM banner_places WHERE id=%d;",$id);
	if (!$res) return null;
	return $res->next();
  }
  
  function getBannerDoc()
  {
	return $this->site->emptyXML("banner");
  }
  
  function getTextBanner($id,$pid,$inScript=false)
  {
	$rw=$this->queryBannerFields($id);
	if (!$rw)
	  return null;
	$doc=$this->getBannerDoc();
	$doc->documentElement->appendChild($doc->createTextNode($rw[2]));
	return $doc;
  }
 
	function checkClass($rw)
	{
	  if (!$rw) return false;
	  if (!$rw[0]) return false;
	  if (empty($rw[0])) return false;
	  return true;
	}
	function checkWH($rw)
	{
	  if (!$rw) return false;
	  if (!$rw[1] || !$rw[2]) return false;
	  if (empty($rw[1]) || empty($rw[2])) return false;
	  if ($rw[1]<1 || $rw[2]<1) return false;
	  return true;
	}
	
	function setClassWH($rw,$obj)
	{
    if ($this->checkClass($rw))
		$this->utils()->xmlHelper()->appendAttribute($obj,"class",$rw[0]);
    if ($this->checkClass($rw))
    {
		$this->utils()->xmlHelper()->appendAttribute($obj,"width",$rw[1]);
		$this->utils()->xmlHelper()->appendAttribute($obj,"height",$rw[2]);
    }
	}
	
	function getScriptNode($doc,$inScript)
	{
	  if ($inScript) 
	    return $doc->documentElement;
	  $scr=$this->utils()->xmlHelper()->appendNode($doc->documentElement,"script");
	  $this->utils()->xmlHelper()->appendAttribute($scr,"type","text/javascript");
	  return $scr;
	}
 
 
 function appendImg($node,$href,$src,$alt)
 {
	$a=$this->utils()->xmlHelper()->appendNode($node,"a");
	$this->utils()->xmlHelper()->appendAttribute($a,"href",$href);
	$img=$this->utils()->xmlHelper()->appendNode($a,"img");
	$this->utils()->xmlHelper()->appendAttribute($img,"src",$src);
	$this->utils()->xmlHelper()->appendAttribute($img,"alt",$alt);
 }
 
  function getImgBanner($id,$pid,$inScript=false)
  {
	$rw=$this->queryBannerFields($id);
	if (!$rw)
	  return null;
	$rw2=$this->queryPlaceFields($pid);
	$doc=$this->getBannerDoc();
	$this->appendImg($doc->documentElement,$rw[0],$rw[1],$rw[3]);
	return $doc;
  }
  
  function getFlashBanner($id,$pid,$inScript=false)
  {
	$rw=$this->queryBannerFields($id);
	if (!$rw)
	  return null;
	$rw2=$this->queryPlaceFields($pid);
	$doc=$this->getBannerDoc();
	$scr=$this->getScriptNode($doc,$inScript);
	$c="";$w=0;$h=0;
	if ($this->checkClass($rw2)) $c=$rw2[0];
	if ($this->checkWH($rw2)) {$w=$rw2[1]; $h=$rw2[2];}
	$scr->appendChild($doc->createTextNode(sprintf("tt_iF('%s','%s',%d,%d);",$rw[2],$c,$w,$h)));
	if (!empty($rw[0]) && !empty($rw[1]) && !empty($rw[3]))
	{
		$nscr=$this->utils()->xmlHelper()->appendNode($doc->documentElement,"noscript");
		$this->appendImg($nscr,$rw[0],$rw[1],$rw[3]);
	}
	return $doc;
  }
  
  function getImgFlashBanner($id,$pid,$inScript=false)
  {
	$rw=$this->queryBannerFields($id);
	if (!$rw)
	  return null;
	$rw2=$this->queryPlaceFields($pid);
	$doc=$this->getBannerDoc();
	$scr=$this->getScriptNode($doc,$inScript);
	$c="";$w=0;$h=0;
	if ($this->checkClass($rw2)) $c=$rw2[0];
	if ($this->checkWH($rw2)) {$w=$rw2[1]; $h=$rw2[2];}
	$scr->appendChild($doc->createTextNode(sprintf("tt_iFI('%s','%s','%s','%s','%s',%d,%d);",$rw[2],$rw[1],$rw[0],$rw[3],$c,$w,$h)));
	$nscr=$this->utils()->xmlHelper()->appendNode($doc->documentElement,"noscript");
	$this->appendImg($nscr,$rw[0],$rw[1],$rw[3]);
	return $doc;
  }
  
  function getScriptBanner($id,$pid,$inScript=false)
  {
    return null;
  }

  function getRndBanner($id,$pid,$inScript=false)
  {
    return null;
  }
  function getContainerBanner($id,$pid,$inScript=false)
  {
    return null;
  }
  
  function processBanner($place_name,$xml,$inScript=false)
  {
    $res=$this->site->common_db()->queryf("SELECT banners.id,banners.type,timelines.priority,banner_places.id FROM objects_in_time,banners,banner_places,timelines WHERE 
		banner_places.place_name='%s' AND banner_places.id=objects_in_time.id1 AND 
		banners.id=objects_in_time.id2 AND timelines.id=objects_in_time.timeline AND
		objects_in_time.type=1 AND objects_in_time.blocked=0 AND timelines.blocked=0 AND
		banners.blocked=0 AND banner_places.blocked=0 AND (timelines.start_date IS NULL OR timelines.start_date<=NOW()) AND
		(timelines.end_date IS NULL OR timelines.end_date>=NOW()) ORDER BY timelines.priority DESC;
		",$place_name);
	if (!$res)
	  return $this->replaceBanner($xml,$place_name,null);
	$rw=$res->next();
	if (!$rw)
		return $this->replaceBanner($xml,$place_name,null);
	$obj=null;
	switch($rw[1])
	{
	  case 1:$obj=$this->getTextBanner($rw[0],$rw[3],$inScript);break;
	  case 2:$obj=$this->getImgBanner($rw[0],$rw[3],$inScript);break;
	  case 3:$obj=$this->getFlashBanner($rw[0],$rw[3],$inScript);break;
	  case 4:$obj=$this->getImgFlashBanner($rw[0],$rw[3],$inScript);break;
	  case 5:$obj=$this->getScriptBanner($rw[0],$rw[3],$inScript);break;
	  case 6:$obj=$this->getRndBanner($rw[0],$rw[3],$inScript);break;
	  case 7:$obj=$this->getContainerBanner($rw[0],$rw[3],$inScript);break;
	}
	return $this->replaceBanner($xml,$place_name,$obj);
  }
  
}

?>